import store from '@/store'

function checkMenu(el, binding) {
  const { value } = binding
  const menus = store.getters && store.getters.menus

  if (value && value instanceof Array) {
    if (value.length > 0) {
      const menuParams = value
      const hasMenu = menus.some(menu => {
        return menuParams.includes(menu)
      })

      if (!hasMenu) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  } else {
    throw new Error(`need node code! Like v-auth="['admin','editor']"`)
  }
}

export default {
  inserted(el, binding) {
    checkMenu(el, binding)
  },
  update(el, binding) {
    checkMenu(el, binding)
  }
}
